home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / windows / games / wincapt.arj / DLGOPEN.C < prev    next >
C/C++ Source or Header  |  1992-04-27  |  11KB  |  326 lines

  1. //**************************************************************************
  2. //
  3. // MODULE  : DLGOPEN.C 
  4. //
  5. // Routine to display a standard COMMDLG File/Save dialog box.
  6. //
  7. // Defines the following functions:
  8. // 
  9. // GetFileName()     - Calls up common dialog file/save, and returns
  10. //                     selected file
  11. //
  12. // Written by Microsoft Product Support Services, Developer Support.
  13. // Copyright (c) 1992 Microsoft Corporation. All rights reserved.
  14. //**************************************************************************/
  15.  
  16. #include <windows.h>
  17. #include "wincap.h"
  18. #include "dialogs.h"
  19. #include "commdlg.h"  // For common dialogs
  20. #include "dlgs.h"     // For common dialog hook proc
  21.  
  22. #define MAXFILENAMELEN 144
  23.  
  24. extern HANDLE ghInst;
  25.  
  26. BOOL FAR PASCAL FileSaveHookProc (HWND hDlg, WORD msg, WORD wParam, LONG lParam);
  27.  
  28. //************************************************************************
  29. // Function:  GetFileName (HWND, LPSTR, LPDWORD)
  30. //
  31. //   Purpose:  Prompts user for a filename through the use of a Windows 3.1
  32. //             FileOpen common dialog box.
  33. //
  34. //Parameters:  HWND hWndOwner      Owner who is calling this funciton
  35. //
  36. //             LPSTR szFileName    Buffer where selected filename will
  37. //                                 be placed.  Must be at least MAXFILENAMELEN
  38. //                                 characters large
  39. //
  40. //             LPDWORD dwFlags     HIWORD is the bits per pixel, LOWORD
  41. //                                 is compression type.
  42. //
  43. //   Returns:  TRUE if a filename is selected.
  44. //             FALSE if no filename is selected.
  45. //
  46. //  Comments:  Filename is put into the string passed to the routine.
  47. //             If a filename is not selected, NULL is returned.
  48. //  History: 
  49. //    Date      Author            Reason
  50. //   06/01/91  Garrett McAuliffe  Created         
  51. //   06/27/91  Eric Flo           Changed OPENFILENAME structure to      
  52. //                                support customized common dialog box      
  53. //   11/21/91  Eric Flo           Changed szFilter to work for NT.      
  54. //   03/09/92  Mark Bader         Added wFlags Parameter so we don't      
  55. //                                have to use globals, changed for WINCAP.      
  56. //
  57. //************************************************************************/
  58.  
  59. BOOL GetFileName (HWND hWndOwner, LPSTR szFileName, LPDWORD dwFlags)
  60. {
  61.    OPENFILENAME   of;
  62.    FARPROC        fpDlgFunc;           // far proc to dlg function
  63.    DWORD          flags;
  64.    static char    szTitle[50];         // Dialog Box title
  65.    static char    szFile[MAXFILENAMELEN];         // File name
  66.    DWORD          dw = 0;               // Local copy of flags for validation
  67.    BOOL           bReturn;             // Return code
  68.    DWORD          dwLocalOpt;
  69.  
  70.    // Parameter validation -- try to recover gracefully if LPWORD
  71.    // is NULL, so we don't die when we try to do a *wFlags = x below
  72.  
  73.    if (dwFlags == NULL)
  74.      dwFlags = &dw;
  75.  
  76.    dwLocalOpt = *dwFlags;  // Make local copy of options
  77.  
  78.    // Initialize the OPENFILENAME members
  79.  
  80.    lstrcpy(szFile, "*.BMP");
  81.  
  82.    LoadString(ghInst, IDS_SAVEASTITLE, (LPSTR)szTitle, 50);
  83.  
  84.    fpDlgFunc = (FARPROC) MakeProcInstance(FileSaveHookProc, ghInst);
  85.  
  86.    flags = OFN_ENABLEHOOK | OFN_ENABLETEMPLATE | OFN_HIDEREADONLY |
  87.            OFN_OVERWRITEPROMPT;
  88.  
  89.    of.lStructSize       = sizeof (OPENFILENAME);
  90.    of.hwndOwner         = hWndOwner;
  91.    of.hInstance         = ghInst;
  92.    of.lpstrFilter       = (LPSTR)"Bitmaps (*.BMP)\0*.TXT\0";
  93.    of.lpstrCustomFilter = NULL;
  94.    of.nMaxCustFilter    = 0L;
  95.    of.nFilterIndex      = 1L;
  96.    of.lpstrFile         = (LPSTR)szFile;
  97.    of.nMaxFile          = MAXFILENAMELEN;
  98.    of.lpstrFileTitle    = NULL;
  99.    of.nMaxFileTitle     = 0;
  100.    of.lpstrInitialDir   = NULL;
  101.    of.lpstrTitle        = szTitle;
  102.    of.Flags             = flags;
  103.    of.nFileOffset       = 0;
  104.    of.nFileExtension    = 0;
  105.    of.lpstrDefExt       = "bmp";
  106.    of.lpfnHook          = fpDlgFunc;
  107.    of.lpTemplateName    = (LPSTR)"FILESAVE";
  108.  
  109.    // Place our flags in the lCustData parameter
  110.    of.lCustData         = dwLocalOpt;
  111.  
  112.    // Call the GetSaveFilename function
  113.  
  114.     if (GetSaveFileName (&of))
  115.       {
  116.  
  117.       // User pressed OK, continue on
  118.       bReturn = TRUE;
  119.  
  120.       // Copy filename out
  121.       lstrcpy (szFileName, of.lpstrFile);
  122.  
  123.       // Copy flags out
  124.       *dwFlags = of.lCustData;
  125.  
  126.       }
  127.       else
  128.       {
  129.         bReturn = FALSE;
  130.         szFileName[0] = '\0';
  131.         *dwFlags = 0;
  132.         }
  133.  
  134.     FreeProcInstance (fpDlgFunc);
  135.     return bReturn;
  136.  
  137. }
  138.  
  139.  
  140. /*************************************************************************
  141.  
  142.   Function:  FileSaveHookProc (HWND, WORD, WORD, LONG)
  143.  
  144.    Purpose:  Hook procedure for FileSave common dialog box.
  145.  
  146.    Returns:  TRUE if message was processed.
  147.              FALSE if the system needs to process the message.
  148.  
  149.   Comments:
  150.  
  151.    History: 
  152.  
  153.       Date      Author          Reason
  154.  
  155.      07/08/91   Eric Flo        Created (SHOWDIB sample)
  156.      11/20/91   Eric Flo        Fixed bug in save.as radio buttons        
  157.      03/09/92   Mark Bader      Used lCustData rather than global DIB info
  158.  
  159. *************************************************************************/
  160.  
  161. BOOL FAR PASCAL FileSaveHookProc (HWND hDlg, WORD msg, WORD wParam, LONG lParam)
  162. {
  163.   HWND      hGroup;
  164.   RECT      rect, DlgRect;
  165.   static LPOPENFILENAME lpOpenFn;
  166.  
  167.   static    DWORD dwFlags;
  168.  
  169.   switch (msg)
  170.     {
  171.     case WM_INITDIALOG:
  172.  
  173.         //  Get the memory handle stored in the extra words.
  174.         //  From this, insert the filename, and the file format
  175.  
  176.         // Retrieve flags which we passed in in lCustData
  177.         lpOpenFn = (LPOPENFILENAME)lParam;
  178.         dwFlags = lpOpenFn->lCustData; 
  179.  
  180.         // Our dwFlags field contains the following:
  181.         //
  182.         // HIWORD: Bits per pixel specification.  Should be set to
  183.         //    one of the following: 1, 4, 8, or 24
  184.         //
  185.         // LOWORD: Compression specification:  Should be set to one of
  186.         //    the following:   IDD_RGB, IDD_RLE4, IDD_RLE8
  187.  
  188.         switch (LOWORD(dwFlags))
  189.           {
  190.           case IDD_RGB:
  191.             SendDlgItemMessage (hDlg, IDD_RGB,  BM_SETCHECK, 1, 0L);
  192.             SendMessage (hDlg, WM_COMMAND, IDD_RGB, 0L);
  193.             break;
  194.  
  195.           case IDD_RLE4:
  196.             SendDlgItemMessage (hDlg, IDD_RLE4, BM_SETCHECK, 1, 0L);
  197.             SendMessage (hDlg, WM_COMMAND, IDD_RLE4, 0L);
  198.             break;
  199.  
  200.           case IDD_RLE8:
  201.           default:
  202.             SendDlgItemMessage (hDlg, IDD_RLE8, BM_SETCHECK, 1, 0L);
  203.             SendMessage (hDlg, WM_COMMAND, IDD_RLE8, 0L);
  204.             break;
  205.           }
  206.  
  207.         switch (HIWORD(dwFlags))
  208.           {
  209.  
  210.           case 4:
  211.             SendDlgItemMessage (hDlg, IDD_4,   BM_SETCHECK, 1, 0L);
  212.             break;
  213.  
  214.           case 8:
  215.             SendDlgItemMessage (hDlg, IDD_8,   BM_SETCHECK, 1, 0L);
  216.             break;
  217.  
  218.           case 24:
  219.             SendDlgItemMessage (hDlg, IDD_24,  BM_SETCHECK, 1, 0L);
  220.             break;
  221.  
  222.           case 1:
  223.           default:
  224.             SendDlgItemMessage (hDlg, IDD_1,   BM_SETCHECK, 1, 0L);
  225.             break;
  226.           }
  227.  
  228.         // Return 1 so Windows sets focus on a control in our
  229.         // dialog.
  230.         return 1;
  231.  
  232.         break;
  233.  
  234.     case WM_COMMAND:
  235.         switch (wParam)
  236.           {
  237.           case IDOK:
  238.             {
  239.             WORD biStyle, biBits;
  240.  
  241.             if (SendDlgItemMessage (hDlg, IDD_RGB, BM_GETCHECK, 0, 0L))
  242.                biStyle = IDD_RGB;
  243.  
  244.             else if (SendDlgItemMessage (hDlg, IDD_RLE4, BM_GETCHECK, 0, 0L))
  245.                biStyle = IDD_RLE4;
  246.  
  247.             else 
  248.                biStyle = IDD_RLE8;
  249.  
  250.             if (SendDlgItemMessage (hDlg, IDD_1, BM_GETCHECK, 0, 0L))
  251.                biBits = 1;
  252.  
  253.             else if (SendDlgItemMessage (hDlg, IDD_4, BM_GETCHECK, 0, 0L))
  254.                biBits = 4;
  255.  
  256.             else if (SendDlgItemMessage (hDlg, IDD_8, BM_GETCHECK, 0, 0L))
  257.                biBits = 8;
  258.  
  259.             else
  260.                biBits = 24;
  261.  
  262.             // Copy result of user input to our lCustData field, which
  263.             // will pass it back to the calling function.
  264.  
  265.             lpOpenFn->lCustData = MAKELONG(biStyle, biBits);
  266.             }
  267.  
  268.             break;
  269.  
  270.           case IDD_FILETYPE:
  271.             hGroup = GetDlgItem (hDlg, IDD_FILETYPEGROUP);
  272.             GetWindowRect (hGroup, &rect);
  273.             GetWindowRect (hDlg, &DlgRect);
  274.             SetWindowPos (hDlg,0, DlgRect.left, DlgRect.top,
  275.                          (DlgRect.right-DlgRect.left),
  276.                           (rect.bottom+(rect.left-DlgRect.left)-DlgRect.top),
  277.                           SWP_NOMOVE | SWP_NOZORDER);
  278.             EnableWindow (GetDlgItem (hDlg, IDD_FILETYPE), 0);
  279.             SetFocus (hGroup);
  280.             break;
  281.  
  282.           case IDD_RLE4:
  283.             if (SendDlgItemMessage (hDlg, IDD_1, BM_GETCHECK, 0, 0L))
  284.                SendDlgItemMessage (hDlg, IDD_1, BM_SETCHECK, 0,0L);
  285.             if (SendDlgItemMessage (hDlg, IDD_8, BM_GETCHECK, 0, 0L))
  286.                SendDlgItemMessage (hDlg, IDD_8, BM_SETCHECK, 0,0L);
  287.             if (SendDlgItemMessage (hDlg, IDD_24, BM_GETCHECK, 0, 0L))
  288.                SendDlgItemMessage (hDlg, IDD_24, BM_SETCHECK, 0,0L);
  289.  
  290.             EnableWindow (GetDlgItem(hDlg, IDD_4), 1);
  291.             SendDlgItemMessage (hDlg, IDD_4, BM_SETCHECK, 1, 0L);
  292.             EnableWindow (GetDlgItem(hDlg, IDD_1), 0);
  293.             EnableWindow (GetDlgItem(hDlg, IDD_8), 0);
  294.             EnableWindow (GetDlgItem(hDlg, IDD_24), 0);
  295.             break;
  296.  
  297.           case IDD_RLE8:
  298.             if (SendDlgItemMessage (hDlg, IDD_1, BM_GETCHECK, 0, 0L))
  299.                SendDlgItemMessage (hDlg, IDD_1, BM_SETCHECK, 0,0L);
  300.             if (SendDlgItemMessage (hDlg, IDD_4, BM_GETCHECK, 0, 0L))
  301.                SendDlgItemMessage (hDlg, IDD_4, BM_SETCHECK, 0,0L);
  302.             if (SendDlgItemMessage (hDlg, IDD_24, BM_GETCHECK, 0, 0L))
  303.                SendDlgItemMessage (hDlg, IDD_24, BM_SETCHECK, 0,0L);
  304.  
  305.             EnableWindow (GetDlgItem(hDlg, IDD_8), 1);
  306.             SendDlgItemMessage (hDlg, IDD_8, BM_SETCHECK, 1, 0L);
  307.             EnableWindow (GetDlgItem(hDlg, IDD_1), 0);
  308.             EnableWindow (GetDlgItem(hDlg, IDD_4), 0);
  309.             EnableWindow (GetDlgItem(hDlg, IDD_24), 0);
  310.             break;
  311.  
  312.           case IDD_RGB:
  313.             EnableWindow (GetDlgItem(hDlg, IDD_1), 1);
  314.             EnableWindow (GetDlgItem(hDlg, IDD_4), 2);
  315.             EnableWindow (GetDlgItem(hDlg, IDD_8), 3);
  316.             EnableWindow (GetDlgItem(hDlg, IDD_24), 1);
  317.             break;
  318.           }
  319.         break;
  320.  
  321.     default:
  322.       break;
  323.     }
  324.   return FALSE;
  325. }
  326.